package javango.contrib.jquery.fields;

import java.lang.annotation.Annotation;

import com.google.inject.Inject;

import javango.contrib.hibernate.HibernateUtil;
import javango.contrib.jquery.annotations.Lookup;
import javango.contrib.jquery.widgets.JQuerySelectWidget;
import javango.contrib.jquery.widgets.JqueryLookupWidget;
import javango.db.Managers;
import javango.forms.fields.CharField;
import javango.forms.widgets.WidgetFactory;

public class JqueryLookupField extends CharField {

	@Inject
	public JqueryLookupField(WidgetFactory widgetFactory) {
		super(widgetFactory);
		setWidget(JqueryLookupWidget.class);
	}

	Class model;

	/**
	 * The string used to format the value shown in the field,  takes string of the format
	 * %{BILLTO}s - shows the BILLTO property of the object as a string. A null value will use the primary key. 
	 */
	String display;
	
	/**
	 * List of properties (comma seperated) that will be used to construct the search form,  matches fields in the Model.
	 * A value of null will use all properties.
	 */
	String search;
	
	/**
	 * List of properties (comma seperated) that will be used to construct the search results,  matched fields in the model.
	 * A value of null will use all properties.
	 */
	String results;

	@Override
	public void handleAnnotation(Annotation annotation) {
		super.handleAnnotation(annotation);
		
		if (annotation instanceof Lookup) {
			Lookup lookup = (Lookup)annotation;
			model = lookup.model();
			display = lookup.display();
			search = lookup.search();			
			results = lookup.results();
		}
	}

	public Class getModel() {
		return model;
	}

	public void setModel(Class model) {
		this.model = model;
	}

	public String getDisplay() {
		return display;
	}

	public void setDisplay(String display) {
		this.display = display;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getResults() {
		return results;
	}

	public void setResults(String results) {
		this.results = results;
	}

}
